package com.example.front.service.impl;

import com.example.front.service.ICategoryService;
import com.example.front.service.ProductService;
import com.example.gok_mall.constUtil.ConstUtil;
import com.example.gok_mall.constUtil.DateTimeUtil;
import com.example.gok_mall.constUtil.PropertiesUtil;
import com.example.front.dao.CategoryDao;
import com.example.front.dao.ProductDao;
import com.example.gok_mall.entity.Category;
import com.example.gok_mall.entity.Product;
import com.example.gok_mall.reponseData.ResponseCode;
import com.example.gok_mall.reponseData.ResultData;
import com.example.front.vo.ProductDetailVo;
import com.example.front.vo.ProductListVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    private ProductDao productDao;

    @Autowired
    private CategoryDao categoryDao;

    @Autowired
    private ICategoryService iCategoryService;

    @Override
    public ResultData<ProductListVo> manageProductDetail(Integer productId) {
        if (productId == null) {
            return ResultData.fail(ResponseCode.ILLEGAL_ARGUMENT.getCode(), ResponseCode.ILLEGAL_ARGUMENT.getCode());
        }
        Product product = productDao.selectByPrimaryKey(productId);
        if (product == null) {
            return ResultData.fail("商品已下架或者删除");
        }
        ProductDetailVo productDetailVo = assembleProductDetailVo(product);
        return ResultData.createData(productDetailVo);
    }
    //对返回的商品详细信息进行封装
    private ProductDetailVo assembleProductDetailVo(Product product) {

        ProductDetailVo productDetailVo = new ProductDetailVo();

        productDetailVo.setId(product.getId());
        productDetailVo.setCategoryId(product.getCategoryId());
        productDetailVo.setName(product.getName());
        productDetailVo.setSubtitle(product.getSubtitle());
        //productDetailVo.setImageHost(PropertiesUtil.getProperty("ftp.server.http.prefix","http://img.happymmall.com/"));
        productDetailVo.setMainImage(product.getMainImage());
        productDetailVo.setSubImages(product.getSubImages());
        productDetailVo.setDetail(product.getDetail());
        productDetailVo.setPrice(product.getPrice());
        productDetailVo.setStock(product.getStock());
        productDetailVo.setStatus(product.getStatus());

        Category category = categoryDao.selectByPrimaryKey(product.getCategoryId());
        if (category == null) {
            productDetailVo.setParentCategoryId(0);//默认根节点
        } else {
            productDetailVo.setParentCategoryId(category.getParentId());
        }
        productDetailVo.setCreateTime(DateTimeUtil.dateToStr(product.getCreateTime()));
        productDetailVo.setUpdateTime(DateTimeUtil.dateToStr(product.getUpdateTime()));
        return productDetailVo;
    }

    @Override
    public List<Product> getProductList(Integer pageNum, Integer pageSize) {
        return null;
    }

    @Override
    public ResultData<PageInfo> getProductList(int pageNum, int pageSize) {
            //startPage--start
            //填充自己的sql逻辑
            //PageHelper--收尾
            PageHelper.startPage(pageNum,pageSize);
            List<Product> productList=productDao.selectList();
            List<ProductListVo> productListVoList= Lists.newArrayList();
            for (Product productItem: productList){
                ProductListVo productListVo=assembleProductListVo(productItem);
                productListVoList.add(productListVo);
            }

            PageInfo pageResult=new PageInfo(productList);
            pageResult.setList(productListVoList);
            return ResultData.createData(pageResult);
        }

        private ProductListVo assembleProductListVo(Product product){
            ProductListVo productListVo=new ProductListVo();

            productListVo.setId(product.getId());
            productListVo.setSubtitle(product.getSubtitle());
            productListVo.setPrice(product.getPrice());
            productListVo.setMainImage(product.getMainImage());
            productListVo.setCategoryId(product.getCategoryId());
            productListVo.setName(product.getName());
            productListVo.setStatus(product.getStatus());
            productListVo.setImageHost(PropertiesUtil.getProperty("ftp.server.http.prefix","http://image.imooc.com/"));

            return productListVo;
        }

    @Override
    public ResultData<ProductDetailVo> getProductDetail(Integer productId) {
        if(productId == null){
            return ResultData.fail(ResponseCode.ILLEGAL_ARGUMENT.getCode(),ResponseCode.ILLEGAL_ARGUMENT.getMage());
        }
        Product product=productDao.selectByPrimaryKey(productId);
        if(product==null){
            return ResultData.fail("商品已下架或者删除");
        }
        if(product.getStatus() != ConstUtil.ProductStatusEnum.ON_SALE.getCode()){
            return ResultData.fail("商品已下架或者删除");
        }
        ProductDetailVo productDetailVo=assembleProductDetailVo(product);
        return  ResultData.createData(productDetailVo);
    }

    @Override
    public ResultData<PageInfo> getProductByKeywordCategory(String keyword, Integer categoryId, int pageNum, int pageSize, String orderBy) {
        if(StringUtils.isBlank(keyword) && categoryId ==null){
            return ResultData.fail(ResponseCode.ILLEGAL_ARGUMENT.getCode(),ResponseCode.ILLEGAL_ARGUMENT.getMage());
        }

        List<Integer> categoryIdList=new ArrayList<>();
        if(categoryId !=null){
            Category category=categoryDao.selectByPrimaryKey(categoryId);

            if(category == null &&StringUtils.isBlank(keyword)){
                //没有该分类，并且还没有关键字，这个时候返回一个空的集合，不能返回报错
                PageHelper.startPage(pageNum,pageSize);
                List<ProductDetailVo> productDetailVoList =Lists.newArrayList();
                PageInfo pageInfo =new PageInfo(productDetailVoList);
                return ResultData.createData(pageInfo);
            }
            categoryIdList = iCategoryService.selectCategoryAndChildrenById(category.getId()).getData();
        }

        if(StringUtils.isNotBlank(keyword)){
            keyword =new StringBuilder().append("%").append(keyword).append("%").toString();
        }

        PageHelper.startPage(pageNum,pageSize);
        //排序处理
        if(StringUtils.isNotBlank(orderBy)){
            if(ConstUtil.ProductListOrderBy.PRICE_ASC_DESC.contains(orderBy)){
                String[] orderByArray=orderBy.split("_");
                PageHelper.orderBy(orderByArray[0]+" "+orderByArray[1]);
            }
        }
        List<Product> productList=productDao.selectByNameAndCategoryIds(StringUtils.isBlank(keyword)?null:keyword,categoryIdList.size()==0?null:categoryIdList);

        List<ProductListVo> productListVoList=Lists.newArrayList();
        for(Product product : productList){
            ProductListVo productListVo=assembleProductListVo(product);
            productListVoList.add(productListVo);
        }

        //开始分页
        PageInfo pageInfo=new PageInfo(productList);

        pageInfo.setList(productListVoList);

        return ResultData.createData(pageInfo);
    }
}
